package monitor;

import config.Config;
import http.HttpUrlConnection;
import model.HTExtractItem;
import model.HTItem;
import monitor.match.BaseMatcher;
import monitor.match.IMatcher;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CanmakeMonitor extends BaseMonitor {
//    private static final String HOST = "https://www.amazon.co.jp/gp/product/B074KDP23Y";
    String HOST = "https://www.amazon.co.jp/%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%A1%E3%82%A4%E3%82%AF-%E3%83%91%E3%83%BC%E3%83%95%E3%82%A7%E3%82%AF%E3%83%88%E3%83%9E%E3%83%AB%E3%83%81%E3%82%A2%E3%82%A4%E3%82%BA03-%E3%82%A2%E3%83%B3%E3%83%86%E3%82%A3%E3%83%BC%E3%82%AF%E3%83%86%E3%83%A9%E3%82%B3%E3%83%83%E3%82%BF-3-3g/dp/B074KDP23Y/ref=sr_1_4?s=beauty&ie=UTF8&qid=1518937725&sr=1-4&keywords=canmake";
    @Override
    public List<HTExtractItem> extractItems() {
        System.out.println("|------------------开始Canmake 03的爬取-------------------|");
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        header.put("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
        header.put("Host", "www.amazon.co.jp");
        header.put("Upgrade-Insecure-Requests","1");
        header.put("Cache-Control", "max-age=0");
        header.put("Connection", "keep-alive");
        header.put("Referer", "https://www.amazon.co.jp/s/ref=nb_sb_noss_1?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&url=search-alias%3Daps&field-keywords=canmake");
        header.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36");
        String response = HttpUrlConnection.getInstance().getString(HOST, null);
        if(TextUtils.isEmpty(response)) return null;
        try {
            ArrayList<HTExtractItem> items = new ArrayList();
            Document doc = Jsoup.parse(response);
            Elements elements = doc.select("#color_name_2_price .a-size-mini");
            for(Element e: elements){
                String itemName = "Canmake 03";
                String price = e.html();
                price = TextUtils.isEmpty(price)?"":price.replaceAll(",", "");
                HTExtractItem item = new HTExtractItem(itemName, price, HOST);
                items.add(item);
                System.out.println("itemName="+itemName +"; price="+price+"; url="+HOST);
            }
            return items;
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("|------------------结束Canmake 03的爬取--------------------|");
        return null;
    }

    @Override
    public List<HTExtractItem> find(List<HTExtractItem> extractedItems) {
        IMatcher matcher = new BaseMatcher();
        return matcher.match(extractedItems);
    }
}
